package com.tongguan.paypos.fragment;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tongguan.paypos.BuildConfig;
import com.tongguan.paypos.R;
import com.tongguan.paypos.activity.LoginActivity;
import com.tongguan.paypos.base.BaseFragment;
import com.tongguan.paypos.common.AppConstants;
import com.tongguan.paypos.common.User;
import com.tongguan.paypos.utils.AppUtils;
import com.tongguan.paypos.utils.SystemUtils;
import com.tongguan.paypos.view.CustomAlertDialog;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Joylive on 2017/1/5 .
 */

public class DeviceInfoFragment extends BaseFragment {

    private static final String PACKAGE_NAME_DEBUG = "com.example.launcher";  //测试版
    private static final String PACKAGE_ACTION_DEBUG = "com.example.launcher.LauncherActivity";

    private static final String PACKAGE_NAME = "com.android.launcher";  //正式版
    private static final String PACKAGE_ACTION = "com.android.launcher2.Launcher";

    @BindView(R.id.tv_device_capacity)
    TextView mTvDeviceCapacity;
    @BindView(R.id.tv_device_capacity_surplus)
    TextView mTvDeviceCapacitySurplus;
    @BindView(R.id.tv_device_username)
    TextView mTvDeviceUsername;
    @BindView(R.id.tv_device_sn)
    TextView mTvDeviceSn;
    @BindView(R.id.tv_device_version)
    TextView mTvDeviceVersion;
    @BindView(R.id.ll_user_tab)
    LinearLayout mLlUserTab;
    @BindView(R.id.ll_launcher)
    LinearLayout mLlLauncher;
    @BindView(R.id.ll_manage)
    LinearLayout mLlManage;
    @BindView(R.id.ll_about)
    LinearLayout mLlAbout;
    private String pkg, cls;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_devie_info, container, false);
        ButterKnife.bind(this, rootView);
        setTitle("设备信息");
        initView();
        initEvent();
        return rootView;
    }

    private void initView() {
        mTvDeviceCapacity.setText(SystemUtils.getSDTotalSize(frmCtx));
        mTvDeviceCapacitySurplus.setText(SystemUtils.getSDAvailableSize(frmCtx));
        mTvDeviceUsername.setText(User.getInstance().getMerchantName());
        mTvDeviceSn.setText(User.getInstance().getPosId());
        mTvDeviceVersion.setText(SystemUtils.getAppVersionName(frmCtx));
    }

    private void initEvent() {
        mLlUserTab.setOnLongClickListener(v -> {
            new CustomAlertDialog.Builder(frmCtx).setTitle(R.string.common_tip_title)
                    .setMessage(getString(R.string.info_tab_user))
                    .setPositiveButton(R.string.common_sure, (dialog, which) -> {
                        User.getInstance().setLoginStatus(false);
                        jumpFrm2Act(LoginActivity.class, true);
                    }).setNegativeButton(R.string.common_cancel, null).show();
            return true;
        });

        mLlLauncher.setOnLongClickListener(v -> {
            new CustomAlertDialog.Builder(frmCtx).setTitle(R.string.common_tip_title)
                    .setMessage(getString(R.string.info_tab_launcher))
                    .setPositiveButton(R.string.common_sure, (dialog, which) -> {
                        if (BuildConfig.isLog) {
                            pkg = PACKAGE_NAME_DEBUG;
                            cls = PACKAGE_ACTION_DEBUG;
                        } else {
                            pkg = PACKAGE_NAME;
                            cls = PACKAGE_ACTION;
                        }
                        ComponentName cName = new ComponentName(pkg, cls);
                        Intent intent = new Intent(Intent.ACTION_MAIN);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.setComponent(cName);
                        startActivity(intent);

                    }).setNegativeButton(R.string.common_cancel, null).show();
            return true;
        });

        mLlManage.setOnLongClickListener(v -> {
            new CustomAlertDialog.Builder(frmCtx).setTitle(R.string.common_tip_title)
                    .setMessage(getString(R.string.info_tab_mtms))
                    .setPositiveButton(R.string.common_sure, (dialog, which) -> AppUtils.runApp(frmCtx, AppConstants.LAUNCHER_APP_STORE))
                    .setNegativeButton(R.string.common_cancel, null).show();
            return true;
        });

        mLlAbout.setOnClickListener(v -> {
            getFragmentManager().beginTransaction().replace(R.id.container, new DeviceInfoAboutFragment()).addToBackStack(null).commit();
        });
    }
}
